﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TMS_Master.Interface;
using TMS_Master.Result;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace TMS_Master.Service
{
    public class PaymentService : TMS_MasterAppService, IPaymentService
    {
        private readonly IRepository<Payment, Guid> _payments;
        public PaymentService(IRepository<Payment, Guid> payments)
        {
            _payments = payments;
        }


        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<PaymentDto> GetPaymentDto(Guid Id)
        {
            var paymentDto = await _payments.GetAsync(Id);
            return ObjectMapper.Map<Payment, PaymentDto>(paymentDto);
        }

        /// <summary>
        /// 显示+分页+查询
        /// </summary>
        /// <param name="Title"></param>
        /// <param name="Ctime"></param>
        /// <param name="CheckType"></param>
        /// <param name="PeoName"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<PaymentDto>> GetPaymentDtos(string Title = "", DateTime? Ctime = null, int CheckType = 0, string PeoName = "", int PageIndex = 0, int PageSize = 5)
        {
            var list = await _payments.ToListAsync();
            list = (from s in list
                    where s.Title.Contains(Title)
                    select s).ToList();
            int cout = list.Count;
            var result = list.Skip((PageIndex - 1) * PageSize).Take(PageSize);
            var res = ObjectMapper.Map<List<Payment>, List<PaymentDto>>(result.ToList());
            return new PagedResultDto<PaymentDto>(cout, res);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="paymentDto"></param>
        /// <returns></returns>
        public async Task<PaymentDto> PaymentCreate(PaymentCreateUpdateDto paymentCreateUpdateDto)
        {
            var payment = ObjectMapper.Map<PaymentCreateUpdateDto, Payment>(paymentCreateUpdateDto);
            var res = await _payments.InsertAsync(payment);
            return ObjectMapper.Map<Payment, PaymentDto>(res);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task PaymentDel(List<Guid> Id)
        {
            for (int i = 0; i < Id.Count; i++)
            {
                await _payments.DeleteAsync(Id[i]);
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="paymentDto"></param>
        /// <returns></returns>
        public async Task<PaymentDto> PaymentUpdate(PaymentCreateUpdateDto paymentCreateUpdateDto)
        {
            var res = ObjectMapper.Map<PaymentCreateUpdateDto, Payment>(paymentCreateUpdateDto);
            var result = await _payments.UpdateAsync(res);
            return ObjectMapper.Map<Payment, PaymentDto>(result);

        }

        /// <summary>
        /// 提交审批+重新提交
        /// </summary>
        /// <param name="id"></param>
        /// +-
        /// <returns></returns>
        public Task<PaymentDto> PaymentUpt(Guid id)
        {
            var res = _payments.FindAsync(id);
            res.Result.Check = (Enum.CheckEnum)1;
            return (Task<PaymentDto>)UnitOfWorkManager.Current.SaveChangesAsync();
        }
    }
}
